//
//  会员购买
//
//  DHBuyMemberViewController.m
//  Dresshere
//
//  Created by ZhangPeng on 2017/12/29.
//  Copyright © 2017年 张朋. All rights reserved.
//

#import "DHBuyMemberViewController.h"
#import "DHPayViewController.h"
#import "DHBuMember_CardCell.h"
#import "DHBuyMember_InfoCell.h"

@interface DHBuyMemberViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIButton *buyButton;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSDictionary *vipCategory;
@property (strong, nonatomic) NSArray *vipListArray;

@property (strong, nonatomic) NSDictionary *couponDic; //购买会员优惠券

@end

@implementation DHBuyMemberViewController

- (NSArray *)vipListArray{
    if (!_vipListArray) {
        _vipListArray = [NSArray array];
    }
    return _vipListArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleLabel.text = @"购买会员";
    
    [self loadVIPCategory];
    [self setupUI];
    [self buttonActions];
    
    // 获取购买会员优惠券
    [self loadVIPPromotion];
}

- (void)setupUI{
    self.view.backgroundColor = [UIColor whiteColor];
    self.buyButton.layer.cornerRadius = self.buyButton.height / 2;

    [self.tableView registerNib:[UINib nibWithNibName:@"DHBuMember_CardCell" bundle:nil] forCellReuseIdentifier:kDHBuMember_CardCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"DHBuyMember_InfoCell" bundle:nil] forCellReuseIdentifier:kDHBuyMember_InfoCell];
}

- (void)buttonActions{
    NSString *coupon_id;
    if (self.couponDic) {
        coupon_id = [self.couponDic objectForKey:@"_id"];
    }
    [self.buyButton bk_addEventHandler:^(id sender) {
        if ([self.vipCategory objectForKey:@"uuid"]) {
            CGFloat couponPrice = 0;
            NSMutableDictionary *params = [NSMutableDictionary dictionary];
            if (coupon_id) {
                [params setObject:coupon_id forKey:@"coupon_id"];
                couponPrice = [[self.couponDic objectForKey:@"discount"] floatValue];
            }
            [params setObject:[self.vipCategory objectForKey:@"uuid"] forKey:@"uuid"];
            
            [[DHNetAPIClient shareDHNetAPIClient] request_vip_purchaseWithParams:params andBlock:^(id data, NSError *error) {
                if (data) {
                    NSDictionary *subscription = [data objectForKey:@"subscription"];
                    double price = [[subscription objectForKey:@"price"] doubleValue];
                    double deposit = [[subscription objectForKey:@"deposit"] doubleValue];
                    NSNumber *totalPrice = [NSNumber numberWithDouble:price + deposit - couponPrice - [GlobalManager shareGlobalManager].deposit];
                    DHPayViewController *payController = [[DHPayViewController alloc] initWithPrice:totalPrice andVipID:[subscription objectForKey:@"vip_uuid"] andVipName:[self.vipCategory objectForKey:@"name"] preOrderId:[subscription objectForKey:@"_id"]];
                    payController.payType = PayTypeVip;
                    [self.navigationController pushViewController:payController animated:YES];
                }
            }];
        }else{
            [NSObject showHudTipStr:@"请选择VIP类型"];
        }
    } forControlEvents:UIControlEventTouchUpInside];
}

- (void)loadVIPCategory{
    [[DHNetAPIClient shareDHNetAPIClient] request_vipLevelsAndBlock:^(id data, NSError *error) {
        self.vipListArray = [data objectForKey:@"vips"];
        self.vipCategory = self.vipListArray[0];
        [self.tableView reloadData];
    }];
}

- (void)loadVIPPromotion{
    [[DHNetAPIClient shareDHNetAPIClient] request_user_vip_promotionAndBlock:^(id data, NSError *error) {
        if (data) {
            self.couponDic = [data objectForKey:@"coupon"];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSource and UITableViewDdelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 150;
    }else{
        return 300;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        DHBuMember_CardCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHBuMember_CardCell forIndexPath:indexPath];
        [cell updateCellArr:self.vipListArray];
        return cell;
    }else{
        DHBuyMember_InfoCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHBuyMember_InfoCell forIndexPath:indexPath];
        [cell updateCell:self.vipCategory];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
